package com.ssm.controller;

import com.ssm.bean.Town;
import com.ssm.service.TownService;
import com.utils.Meg;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TownController {
    @Autowired
    private TownService townService;

    @RequestMapping("/townView")
    public String townView(){
        return "town/town";
    }

    @RequestMapping("/townEditView")
    public String townEditView(){
        return "town/townPage";
    }

    @RequestMapping("/townAddView")
    public String townAddView(){
        return "town/townAdd";
    }

    @ResponseBody
    @RequestMapping("/townData")
    public Meg townData(@Param("page") String page, @Param("limit") String limit){
        return Meg.success().add("data",townService.selectAll(limit,page));
    }

    @ResponseBody
    @RequestMapping("/townInsert")
    public Meg townInsert(Town town){
        int i = townService.insert(town);
        if (i>0)
            return Meg.success();
        else
            return  Meg.file();
    }

    @ResponseBody
    @RequestMapping("/townUpdate")
    public Meg townUpdate(Town town){
        int i = townService.update(town);
        if (i>0)
            return Meg.success();
        else
            return  Meg.file();
    }

    @ResponseBody
    @RequestMapping("/townDelete")
    public Meg townDelete(@Param("id") Integer id){
        int i = townService.delete(id);
        if (i>0)
            return Meg.success();
        else
            return  Meg.file();
    }

    @ResponseBody
    @RequestMapping("/getTownByDiPk")
    public Meg getTownByDiPk(@RequestParam("diPk") Integer diPK ){
        return Meg.success().add("data",townService.getByDiPk(diPK));
    }
}
